grApp.filter('isSign',function(){
    var result='';
    return function(item,signDay){
        if(item===0){
            result='未签到';
        }else{
            result='签到'+signDay+'天';
        }
        return result;
    };
});
var flag = 0;
//首页操作的控制器
grApp.controller('HomeCtrl',['$scope','BaseService','toaster',function($scope,BaseService,toaster){

    $scope.sign = function(){
        //alert('>_< ! 抱歉哦,亲 ! \n该功能升级中,程序猿正在疯狂的写代码... 等等有惊喜!~');
        if($scope.isSignToday == 1){
            window.location.href="/activity/signday";
        }else {
            //判断当天是否已签到过
            BaseService.isSignedTody().then(function(data){
                if(data.code == 0 && data.SignState == 0){
                    //未签到
                    BaseService.setSignDayAction().then(function (result) {
                        if(result.code === 0){
                            $scope.isSignToday = 1;
                            $scope.allSignDay = result.AllSignDays ;
                        }else{
                            $scope.isSignToday = 0;
                        }
                    }).catch(function(err){
                        $scope.isSignToday = 0;
                        //logger.error(err);
                    });
                }else if(data.code == 0 && data.SignState == 1){
                    $scope.isSignToday = 1;
                    $scope.allSignDay = data.AllSignDays ;
                }else{
                    //可能出错了
                    $scope.isSignToday = 0;
                }
            })
        }
    };
    $scope.addToCart = function(pid,sid,num){
        BaseService.addToCart(pid,sid,num).then(function(res){
            //console.log(res.reason.code);
            if(res.code==259){
                if(res.reason.code==-1){
                    toaster.error('库存不足');
                }
            }else{
                toaster.success('添加成功');
            }
        }).catch(function(err){
            toaster.error('添加失败');
        });
    }
    $scope.load = function(isSign,allSignDay){
        //初始化时获取isSignToday的状态
        // result.SignState ===0  未签到 ===1是已签到
        //$scope.isSignToday = isSign ;
        //$scope.allSignDay = allSignDay ;
    }

    //签到按钮点击事件
    $scope.signBtn = function () {

    };
    $scope.goToWM = function(){
        //href="/juiceHome"
        var startDate = new Date(new Date().toDateString() + " " + "9:30:00");
        var endDate = new Date(new Date().toDateString() + " " + "17:00:00");
        if(startDate <= new Date() && new Date()<=endDate){
            window.location.href = "/juiceHome" ;
        }else{
            alert('稍等片刻，外卖即将开始,外卖时间：9:30~17:00');
        }
    }

    $scope.goToWM = function(){
        //href="/juiceHome"
        var startDate = new Date(new Date().toDateString() + " " + "9:30:00");
        var endDate = new Date(new Date().toDateString() + " " + "17:00:00");
        if(startDate <= new Date() && new Date()<=endDate){
            window.location.href = "/juiceHome" ;
        }else{
            alert('派送时间：9:30~17:00');
            window.location.href = "/juiceHome" ;
        }
    }

}]);
